package com.foreverwin.mes.lims.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.lims.service.DelayInactiveService;
import com.foreverwin.mes.lims.model.DelayInactive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Max
 * @since 2020-03-16
 */
@Controller
@RequestMapping("/delayInactives")
public class DelayInactiveController {
    private final Logger logger = LoggerFactory.getLogger(DelayInactiveController.class);

    @Autowired
    public DelayInactiveService delayInactiveService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getDelayInactiveById(@PathVariable String id) {
        DelayInactive result;
        try {
            result = delayInactiveService.selectById(id);
        } catch (Exception e) {
            logger.error("getDelayInactiveById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getDelayInactiveList(DelayInactive delayInactive){
        List<DelayInactive> result;
        try {
            EntityWrapper<DelayInactive> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(delayInactive);
            result = delayInactiveService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getDelayInactiveList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getDelayInactivePageList")
    public AjaxResult getDelayInactivePageList(FrontPage<DelayInactive> frontPage, DelayInactive delayInactive){
        Page result;
        try {
            String site = CommonMethods.getSite();
            EntityWrapper<DelayInactive> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(delayInactive);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("handle", frontPage.getGlobalQuery())
                    .or().like("zItemTank", frontPage.getGlobalQuery())
                    .or().like("resrce", frontPage.getGlobalQuery())
                    .or().like("resrceDesc", frontPage.getGlobalQuery())
                    .or().like("reason", frontPage.getGlobalQuery())
        ;
            }
            result = delayInactiveService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getDelayInactivePageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param delayInactive  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult delayInactiveSave(@RequestBody DelayInactive delayInactive) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            //count = delayInactiveService.insertOrUpdate(delayInactive) ? 1 : 0;
            count = delayInactiveService.delayActiveTime(delayInactive ,user);
        } catch (Exception e) {
            logger.error("delayInactiveSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : delayInactive);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult delayInactiveDelete(@PathVariable("id") String id){
        int count;
        try {
            count = delayInactiveService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("delayInactiveDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = delayInactiveService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("delayInactiveBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}